package cn.wikiflyer.beikang.act.user;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.IApplication;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.user.bean.CardListBean;
import cn.wikiflyer.beikang.act.user.bean.CardListBean.CardBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.view.WKHeader;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 收支详情——提现
 * 
 * @author qunlee_mr
 * 
 */
public class UserBalanceCashAct extends BaseActivity {

	// @ViewInject(id = R.id.header)
	// private WKHeader header;

	@ViewInject(id = R.id.card_over)
	private TextView card_over;

	@ViewInject(id = R.id.cash_btn)
	private Button cash_btn;

	@ViewInject(id = R.id.card_cash)
	private EditText card_cash;

	@ViewInject(id = R.id.card_text)
	private TextView card_text;

	@ViewInject(id = R.id.selectcard)
	private View selectcard;

	@ViewInject(id = R.id.left_img)
	private View left;

	@ViewInject(id = R.id.right_txt)
	private View right;

	@Override
	public void loadXml() {
		setContentView(R.layout.act_user_balance_cash);
	}

	@Override
	public void init() {
		// TextView rightTxt = header.getRightTxt();
		// rightTxt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
		// rightTxt.setText("＋ 添加银行卡");

		// header.setClickListener(null, null, new OnClickListener() {
		// public void onClick(View arg0) {
		// startActivity(new Intent(ctx, UserBalanceCardListAct.class));
		// }
		// });

		left.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				onBackPressed();
			}
		});
		right.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(ctx, UserBalanceCardListAct.class));
			}
		});

		card_over.setText("" + IApplication.self.userInfo.money);
		card_cash.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				checkBtn(s.toString());
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
			}
		});

		cash_btn.setEnabled(false);

		cash_btn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				submit();
			}
		});

		card_text.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(ctx, UserBalanceCardListAct.class);
				i.putExtra("select", true);
				startActivity(i);
			}
		});
		selectcard.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(ctx, UserBalanceCardListAct.class);
				i.putExtra("select", true);
				startActivity(i);
			}
		});

		new UpdateCard(this);
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		if (cardBean != null) {
			card_text.setText("" + cardBean.bankName + "("
					+ cardBean.cardNumber + ")");
			cardid = cardBean.id;
		} else {
			card_text.setText("");
			cardid = null;
			new UpdateCard(this);
		}
	}

	private void submit() {
		if (cardid == null || cardid.equals("")) {
			IApplication.showToast("请先选择银行卡");
			return;
		}
		new GiveMeMoney(ctx);
	}

	private void checkBtn(String s) {
		if (s == null || s.equals("")) {
			cash_btn.setEnabled(false);
			return;
		}
		int cur = Integer.valueOf(s);
		Float over = Float.valueOf(IApplication.self.userInfo.money);
		cash_btn.setEnabled(over >= cur);
	}

	@Override
	public void setData() {
	}

	/**
	 * 更新
	 */
	private void updateCard() {
		if (bean == null || bean.list == null || bean.list.size() == 0) {
			return;
		}
		CardBean cb = null;
		for (int i = 0; i < bean.list.size(); i++) {
			cb = bean.list.get(i);
			if (cb.is_default.equals("1")) {
				cardid = cb.id;
				card_text.setText("" + cb.bankName + "(" + cb.cardNumber + ")");
				return;
			}
		}
		cb = bean.list.get(0);
		card_text.setText("" + cb.bankName + "(" + cb.cardNumber + ")");
		cardid = cb.id;
	}

	/************************** 接口相关 ***********************************/

	private CardListBean bean = null;
	public static CardBean cardBean = null;
	private String cardid = null;

	/**
	 * 获取银行卡列表
	 */
	class UpdateCard extends WKAsyncTaskPro {
		public UpdateCard(Context context) {
			super(context, false);
		}

		@Override
		protected String doInBackground(boolean... isLocal) {
			return app().net.getBankCards(isLocal);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean<CardListBean>>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean<CardListBean> base = gson.fromJson(result, cvbType);
			bean = base.data;
			updateCard();
		}
	}

	/**
	 * 提现
	 */
	class GiveMeMoney extends WKAsyncTaskPro {
		public GiveMeMoney(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... isLocal) {
			String money = "" + card_cash.getText().toString();
			return app().net.giveMeMoney(money, cardid);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				IApplication.showToast("申请成功");
				finish();
			} else {
				Toast.makeText(ctx, "获取数据失败\n" + base.info, Toast.LENGTH_SHORT)
						.show();

			}
		}
	}

}
